
package com.chongchong.multifile.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chongchong.core.boot.ctrl.SysController;
import com.chongchong.core.mp.support.Condition;
import com.chongchong.core.mp.support.Query;
import com.chongchong.core.secure.SysUser;
import com.chongchong.core.tool.api.R;
import com.chongchong.core.tool.utils.StringUtil;
import com.chongchong.multifile.entity.FileList;
import com.chongchong.multifile.service.IFileListService;
import com.chongchong.multifile.utils.ExcelUtil;
import com.chongchong.multifile.vo.FileListVO;
import com.chongchong.multifile.wrapper.FileListWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 文件分块上传历史表 控制器
 *
 * @author en
 * @since 2022-07-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fileList")
@Api(value = "文件分块上传历史表", tags = "文件分块上传历史表接口")
public class FileListController extends SysController {

	private IFileListService fileListService;

	/**
	 * 查询已上传文件列表列表
	 */
	@ApiOperation("查询已上传文件列表")
	@GetMapping("/list")
	public R<IPage<FileList>> list(FileList filelist, Query query)
	{
		IPage<FileList> pages = fileListService.page(Condition.getPage(query), Condition.getQueryWrapper(filelist));
		return R.data(pages);
	}
	/**
	 * 查询已上传文件列表列表
	 */
	@ApiOperation("根据id查询已上传文件列表")
	@GetMapping("/listByIds")
	public R<List<FileListVO>> listByIds(@RequestParam String ids,SysUser user){
		List<FileList> list = null;
		if(StringUtil.isNotBlank(ids)){
			list= fileListService.selectFileListByIds(ids);
		}

		return R.data(FileListWrapper.build().listVO(list));
	}
	/**
	 * 导出已上传文件列表列表
	 */
	@ApiOperation("导出已上传文件列表列表")
	@GetMapping("/export")
	public R export(FileList  fileList)
	{
		List<FileList> list = fileListService.selectFileListList(fileList);
		ExcelUtil<FileList> util = new ExcelUtil<FileList>(FileList.class);
		boolean r = (Integer)util.exportExcel(list, "fileList").get("code") == 200?true:false;
		return R.status(r);
	}

	/**
	 * 获取已上传文件列表详细信息
	 */
	@ApiOperation("获取已上传文件列表详细信息")
	@GetMapping(value = "/{id}")
	public R<FileListVO> getInfo(@PathVariable("id") Long id)
	{
		FileList fileList = fileListService.selectFileListById(id);
		return R.data(FileListWrapper.build().entityVO(fileList));
	}

	/**
	 * 新增已上传文件列表
	 */
	@ApiOperation("新增已上传文件列表")
	@PostMapping
	public R<Long> add(@RequestBody FileList  fileList)
	{
		fileListService.insertFileList(fileList);
		return R.data(fileList.getId());
	}

	/**
	 * 修改已上传文件列表
	 */
	@ApiOperation("修改已上传文件列表")
	@PostMapping("/updateFile")
	public R<FileListVO> edit(@RequestBody FileList  fileList, SysUser user)
	{
		fileList.setUpdateTime(new Date());
		if(user != null){
			fileList.setUpdateUserId(user.getUserId());
			//审核信息
			if(fileList.getAuditStatus() != null && fileList.getAuditStatus()>0) {
				fileList.setAuditUserId(user.getUserId());
				fileList.setAuditTime(new Date());
			}
		}
		int count = fileListService.updateFileList(fileList);
		boolean r = count >0 ? true: false;
        if(r){
			return R.data(FileListWrapper.build().entityVO(fileListService.getById(fileList.getId())));
		}
		return R.status(r);
	}

	/**
	 * 删除已上传文件列表
	 */
	@ApiOperation("删除已上传文件列表")
	@DeleteMapping("/{ids}")
	public R remove(@PathVariable Long[] ids)
	{
		int count = fileListService.deleteFileListByIds(ids);
		boolean r = count >0 ? true: false;
		return R.status(r);
	}

}
